#include "child_info.h"
#include "ui_child_info.h"

child_info::child_info(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::child_info)
{
    ui->setupUi(this);

    /*
     * 样式设计部分
     */
    setAttribute(Qt::WA_QuitOnClose,false);
    this->setWindowTitle("儿童资料");
    this->setStyleSheet("background-color: #E2EEF7");
    ui->pushButton_new->setStyleSheet("color: white; background-color: #55AAFF");
    ui->pushButton_search->setStyleSheet("color: white; background-color: #55AAFF");

    sql = QSqlDatabase::addDatabase("QSQLITE", "second");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        qDebug() << "数据库连接成功";
    }
    else{
        qDebug() << "数据库连接失败";
    }
    ui->tableWidget->setColumnCount(7);
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("儿童ID"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("儿童姓名"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("性别"));
    ui->tableWidget->setHorizontalHeaderItem(3,new QTableWidgetItem("出生日期"));
    ui->tableWidget->setHorizontalHeaderItem(4,new QTableWidgetItem("详情"));
    ui->tableWidget->setHorizontalHeaderItem(5,new QTableWidgetItem("筛查"));
    ui->tableWidget->setHorizontalHeaderItem(6,new QTableWidgetItem("删除"));

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    //列宽自适应内容
    ui->tableWidget->resizeColumnsToContents();
      //隐藏表格的列标号
    ui->tableWidget->verticalHeader()->setVisible(false);

    //设置表格不可编辑
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->lineEdit->setText("小明");
    ui->comboBox->addItem("精确查询");
    ui->comboBox->addItem("模糊查询");
    ui->comboBox->addItem("全部");

    /*
     * 信号连接部分
     */
    connect(this, SIGNAL(sign_del(int)), this, SLOT(del_child_info(int)));
    connect(ui->pushButton_new, SIGNAL(clicked(bool)), this, SLOT(Ready_new()));
    connect(ui->pushButton_search, SIGNAL(clicked(bool)), this, SLOT(to_search()));
}


void child_info::display_child_info(){
    this->show();
}


void child_info::to_search(){
        QSqlQuery query(sql);
        //执行sql查询操作
        QString sql_text;
        if(ui->comboBox->currentIndex() == 0){
            sql_text = "select * from child_data where name='" + ui->lineEdit->text() + "';";
        }
        else if(ui->comboBox->currentIndex() == 2){
            sql_text = "select * from child_data;";
        }
        else{
            sql_text = "SELECT * FROM child_data WHERE name LIKE '%" + ui->lineEdit->text() + "%' ;";
        }
        query.exec(sql_text);
        Print_Rows(query);//查询
}


void child_info::Print_Rows(QSqlQuery &query)
{

    int i=0;
    while(query.next()) i++;    //统计行数

    ui->tableWidget->setRowCount(i);
    if(query.isActive())    query.seek(-1);

    i=0;
    QPushButton* btn1 = NULL;
    QPushButton* btn2 = NULL;
    QPushButton* btn3 = NULL;
    while(query.next())//依次取出下一行数据
    {
        btn1 = new QPushButton(this);
        btn2 = new QPushButton(this);
        btn3 = new QPushButton(this);

        connect(btn1, SIGNAL(clicked(bool)), this, SLOT(detailed_info()));
        connect(btn2, SIGNAL(clicked(bool)), this, SLOT(Ready_test()));
        connect(btn3, SIGNAL(clicked(bool)), this, SLOT(Ready_del()));

        btn1->setText("查看详情");
        btn2->setText("开始筛查");
        btn3->setText("删除记录");
        btn1->setStyleSheet("background-color: #55AAFF; color: white");
        btn2->setStyleSheet("background-color: #00DD00; color: white");
        btn3->setStyleSheet("background-color: #C70063; color: white");

        QString id = query.value(0).toString();
        QString name = query.value(1).toString();
        QString sex = query.value(2).toString();
        QString birth = query.value(3).toString();
        qDebug()<< id <<"," << name << "," << sex << "," << birth;
        //将查询到的数据插入到表格里
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(id));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(name));
        if(sex.toInt() == 0)    ui->tableWidget->setItem(i,2,new QTableWidgetItem("女"));
        else    ui->tableWidget->setItem(i,2,new QTableWidgetItem("男"));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(birth));
        ui->tableWidget->setCellWidget(i,4, btn1);
        ui->tableWidget->setCellWidget(i,5, btn2);
        ui->tableWidget->setCellWidget(i,6, btn3);

        id_list.append(id);
        name_list.append(name);
        i++;
    }
}

void child_info::detailed_info(){
    int index = ui->tableWidget->currentRow();
    QString id = id_list.at(index);
    qDebug() << id.toInt();
    emit open_info(id.toInt());
}


void child_info::del_child_info(int id){

    QString del_str = "DELETE FROM child_data WHERE id=" + QString::number(id);
    qDebug() << "str = " << del_str;
    QSqlQuery query(sql);
    bool ok = query.exec(del_str);
    if(ok == true){
        QMessageBox::information(this, "删除成功", "删除成功", "确认", "取消");
    }
    else{
        QMessageBox::warning(this, "删除失败", "删除失败", "确认", "取消");
    }
}

void child_info::Ready_del(){
    int index = ui->tableWidget->currentRow();
    QString id = id_list.at(index);
    emit sign_del(id.toInt());
}

void child_info::Ready_new(){
    emit open_new();
}

void child_info::Ready_test(){
    int index = ui->tableWidget->currentRow();
    QString id = id_list.at(index);
    QString name = name_list.at(index);
    emit open_test(id.toInt(), name);
}

child_info::~child_info()
{
    delete ui;
}
